package com.ykdl.member.kid.circle;

import java.util.ArrayList;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.beans.Memberbean;
import com.ykdl.member.kid.beans.Memberlistbean;
import com.ykdl.member.kid.beans.UserActorBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.person.PersonCenterActivity;
import com.ykdl.member.kid.util.DateUtil;
import com.ykdl.member.kid.widget.AsyncImageView;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class MemberListActivity extends BaseScreen{
	private RefreshListView member_list ;
	private ArrayList<UserActorBean>  memberlist;
	private MemberListAdapter adapter ;
	private String TAG = "MemberListActivity";
	private String groupid ="" ;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("圈成员", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		groupid = this.getIntent().getStringExtra("groupid");
		setContent(R.layout.memberlistactivity_xml);
		
		inint(groupid);
	}
	public void inint(String groupid) {
		member_list = (RefreshListView) findViewById(R.id.member_list);
		adapter = new MemberListAdapter();
		showProgress("正在发送请稍等...");
		String  pageurl =KidConfig.GET_GROUP_MEMBER+groupid ;
		HttpBaseRequest request  =  Wxxr.getInstance().getBaseRequest(pageurl);
		 TaskManager.startHttpRequest(request, new getMemberListTag(),  Memberlistbean.class);
	}
	
	class getMemberListTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			if (t instanceof Memberlistbean) {
				finishProgress();
				Memberlistbean bean = (Memberlistbean)t;
				memberlist = bean.getList();
				adapter.setList(memberlist);
				adapter.notifyDataSetChanged();
				member_list.setAdapter(adapter);
				Toast.makeText(mContext, "成功", 1).show();
				}
			
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	class MemberListAdapter extends BaseAdapter {
		
		ArrayList<UserActorBean>   list;
		
		public void setList(ArrayList<UserActorBean> list) {
			this.list = list;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if (list==null) {
				return 0;
			}else {
				return list.size();
			}
			
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder;
			if (convertView == null) {
				convertView = RelativeLayout.inflate(mContext,
						R.layout.circle_member_item, null);
				holder = new ViewHolder();
				holder.head = (AsyncImageView) convertView
						.findViewById(R.id.head);
				holder.name = (TextView) convertView.findViewById(R.id.name);
				holder.age = (TextView) convertView.findViewById(R.id.age);
				holder.adress = (TextView) convertView
						.findViewById(R.id.adress);
				holder.signname = (TextView) convertView
						.findViewById(R.id.signname);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			if (list.get(position).getDisplay_name() != null) {
				holder.name.setText(list.get(position).getDisplay_name() + "");
			}
			if (list.get(position).getProfile() != null) {
				holder.adress.setText(list.get(position).getProfile()
						.getRegion());
				holder.signname.setText(list.get(position).getProfile()
						.getSignature());
				holder.age.setText(DateUtil.getDateDataSting(list.get(position)
						.getProfile().getBirthday()));
			}
			if (list.get(position).getAvatar_file_meta() != null) {
				String url = list.get(position).getAvatar_file_meta()
						.getDownload_urls().getSmal().getUrl();
				holder.head.setUrl(TAG, url, ImageOptionsFactory
						.getSmallPortraitOptions(MemberListActivity.this));
			}
			final UserActorBean bean = list.get(position) ;
			holder.head.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					go(PersonCenterActivity.class,
							PersonCenterActivity.STR_ACTOR_ID, bean.getActor_id());
				}
			});

			return convertView;
		}
		
	}
	class ViewHolder {
		AsyncImageView head;
		TextView name;
		TextView age;
		TextView adress;
		TextView signname;
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().setCurrentActivity(TAG);
		super.onResume();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().pause(TAG);
		super.onPause();
	}
}
